package com.panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.main.TrainFrame;
import com.objects.Train;
import com.objects.Wagon;
import com.panels.cell.ImageValueComboBoxCellEditor;
import com.panels.cell.ImageValueComboBoxCellRenderer;
import com.panels.cell.ValueComboBoxCellEditor;
import com.panels.cell.ValueComboBoxCellRenderer;

public class EditTrainPanel extends JDialog implements ActionListener{

	private JPanel center;
	
	int WIDTH=300;
	int HEIGHT=550;
	private JButton save_train;
	private JTable trains_table;
	private JTable wagons_table;
	private JButton add_wagon;
	private JButton delete_wagon;
	
	public Train[] cloned_trains=null;
	
	public boolean modified=false;
	
	TrainFrame trainFrame=null;

	public EditTrainPanel(TrainFrame trainFrame) {
		
		this.trainFrame=trainFrame;
		
		setTitle("Build edit train");
				
		setSize(WIDTH,HEIGHT);
		center=new JPanel(null);
		setModal(true);
		
		modified=false;
		cloned_trains=null;
		
		int r=10;	
		
		add(BorderLayout.CENTER,center);
		
		cloned_trains=new Train[trainFrame.trains.length];
		
		for (int i = 0; i < trainFrame.trains.length; i++) {
			
			try {
				
				cloned_trains[i]=(Train) trainFrame.trains[i].clone();
				
			} catch (CloneNotSupportedException e1) {
				e1.printStackTrace();
			}
		}
		
		trains_table=new JTable(){
			
			public boolean isCellEditable(int row, int column) {
				return true;
			}
			
		};
		
		 
		trains_table.addMouseListener(
				
				
				new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mousePressed(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseExited(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseEntered(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseClicked(MouseEvent e) {
						
						 int count=e.getClickCount();
						 
						 trainDetail();

						
					}

			
				}
		
		
				
		);
		
		

		
		DefaultTableModel model = buildTableModel(cloned_trains);		
		trains_table.setModel(model);
		
		JScrollPane jscp=new JScrollPane(trains_table); 
		
		jscp.setBounds(10,r,250,200);
		center.add(jscp);
		
		r+=210;
		
		save_train=new JButton("Save train");
		save_train.setBounds(10,r,100,20);
		save_train.addActionListener(this);
		center.add(save_train);
		
		r+=30;
		
		wagons_table=new JTable(){
			
			public boolean isCellEditable(int row, int column) {
				return true;
			}
		
			
		};
		

		DefaultTableModel model2 = buildWagonsModel(null);		
		wagons_table.setModel(model2);
		setWagonCellComponents();
		
		
		JScrollPane jscp2=new JScrollPane(wagons_table); 
		
		jscp2.setBounds(10,r,250,200);
		center.add(jscp2);
		
		r+=210;
		
		add_wagon=new JButton("Add");
		add_wagon.setBounds(30,r,100,20);
		add_wagon.addActionListener(this);
		center.add(add_wagon);
		
		delete_wagon=new JButton("Delete");
		delete_wagon.setBounds(140,r,100,20);
		delete_wagon.addActionListener(this);
		center.add(delete_wagon);
		
		setVisible(true);
	}
	
	private void setWagonCellComponents() {
		
		ImageIcon[] icons = Wagon.getWagonImages();
		
		Vector wagon_types=Wagon.getWagonTypesList();
		wagons_table.getColumnModel().getColumn(0).setCellRenderer(new ImageValueComboBoxCellRenderer(wagon_types,false,icons));
		wagons_table.getColumnModel().getColumn(0).setCellEditor(new ImageValueComboBoxCellEditor(wagon_types,false,icons));
		//wagons_table.getColumnModel().getColumn(0).setCellRenderer(new ValueComboBoxCellRenderer(wagon_types,false));
		//wagons_table.getColumnModel().getColumn(0).setCellEditor(new ValueComboBoxCellEditor(wagon_types,false));

	}

	private void trainDetail() {
		
		int ind=trains_table.getSelectedRow();
		
		if(ind<0)
			return;	
		
		Train train=(Train) cloned_trains[ind];
		
		DefaultTableModel model2 = buildWagonsModel(train);		
		wagons_table.setModel(model2);
		setWagonCellComponents();
		
	}



	private DefaultTableModel buildWagonsModel(Train train) {
	
		//System.out.println("buildWagonsModel");
		
		DefaultTableModel model=new DefaultTableModel();
		
		Vector columns=new Vector();
		columns.add("Wagon type");
		
		Vector data=new Vector();
		
		model.setDataVector(data,columns);
		
		if(train!=null){
			
			for (int i = 0; i < train.wagons.length; i++) {
				
				Wagon w = train.wagons[i];
				
				Vector record=new Vector();
				record.add(""+w.wagon_type);
				
				data.add(record);
			}
			
			
		}
		
		

		
		return model;
	}

	private DefaultTableModel buildTableModel(Train[] cloned_trains) {
		
		
		DefaultTableModel model=new DefaultTableModel();
		
		Vector columns=new Vector();
		columns.add("Code");
		
		Vector data=new Vector();
		
		for (int i = 0; i < cloned_trains.length; i++) {
			
			Vector record=new Vector();
			
			Train tr= cloned_trains[i];
			
			record.add(" "+tr.getTrainCode());
			
			data.add(record);
			
		}	
		
		model.setDataVector(data,columns);
		


		return model;
	}

	public void actionPerformed(ActionEvent arg0) {
		
		Object obj = arg0.getSource();
		
		if(obj==save_train){
			
			saveTrain(); 
		}
		else if(obj==add_wagon){
			
			
			addWagon();
		}
		else if(obj==delete_wagon){

			deleteWagon();
			
		}
		
		
		
		
	}

	private void saveTrain() {
	
		
		int ind=trains_table.getSelectedRow();
		
		if(ind<0)
			return;	
		
		modified=true;
		
		Vector data=((DefaultTableModel)trains_table.getModel()).getDataVector();
		
		Vector record=(Vector) data.elementAt(ind);
		
		String new_code=(String) record.elementAt(0);
		
		Train cTrain=cloned_trains[ind];
		
		cTrain.setTrainCode(new_code);
		
		Vector wdata=((DefaultTableModel)wagons_table.getModel()).getDataVector();
		
		for (int i = 0; i < wdata.size(); i++) {
			
			Vector wRecord = (Vector) wdata.elementAt(i);
			
			String new_type=(String)wRecord.elementAt(0);
			
			cTrain.wagons[i].setWagon_type(Integer.parseInt(new_type));
		}
		
		dispose();
		
	}

	private void deleteWagon() {
		
		int ind=trains_table.getSelectedRow();
		
		if(ind<0)
			return;	
		
		Train train= cloned_trains[ind];
		
		int ind2=wagons_table.getSelectedRow();
		
		if(ind2<0)
			return;
		
		train.deleteWagon(ind2);
		
		updateTables();
		
	}



	private void addWagon() {
		
		int ind=trains_table.getSelectedRow();
		
		if(ind<0)
			return;	
		
		Train train= cloned_trains[ind];
		
		Wagon w=new Wagon(trainFrame);
		
		if(train.wagons.length>0){
			
			
			Wagon lw=train.wagons[train.wagons.length-1];
			
			w.cm= lw.getBackCenter().clone();
			w.fi=lw.fi;
			
			double len=-(lw.semiLen+Wagon.link_distance);
			
			w.cm.translate(len*Math.cos(w.fi),len*Math.sin(w.fi),0);

			
		}
		
		train.addWagon(w,train.wagons.length);
		
		updateTables();
	}
	
	private void updateTables() {
		
		int indx=trains_table.getSelectedRow();
   
		DefaultTableModel model = buildTableModel(cloned_trains);		
		trains_table.setModel(model);

				
		if(indx>=0){
			
			trains_table.setRowSelectionInterval(indx,indx);
			Train train= cloned_trains[indx];
			
			DefaultTableModel model2 = buildWagonsModel(train);		
			wagons_table.setModel(model2);

			
		}else{
			
			DefaultTableModel model2 = buildWagonsModel(null);		
			wagons_table.setModel(model2);
			
		
			
		}
		
		setWagonCellComponents();

	}

}
